package net.zhuike.novel.app.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.ui.activity.AboutActivity;
import net.zhuike.novel.app.ui.activity.CacheActivity;
import net.zhuike.novel.app.ui.activity.DisclaimerActivity;
import net.zhuike.novel.app.ui.activity.FileSystemActivity;
import net.zhuike.novel.app.ui.activity.FontActivity;
import net.zhuike.novel.app.ui.activity.ReadHelpActivity;
import net.zhuike.novel.app.ui.activity.WebViewActivity;
import net.zhuike.novel.app.ui.activity.WifiBookActivity;
import net.zhuike.novel.app.ui.activity.anount.LoginActivity;
import net.zhuike.novel.app.ui.base.BaseFragment;
import net.zhuike.novel.app.ui.base.Constant;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;
import net.zhuike.novel.app.ui.view.alertview.AlertView;
import net.zhuike.novel.app.ui.view.alertview.OnItemClickListener;

import butterknife.Bind;
import framework.common.zanetwork.UeHttpUrl;

/**
 * Created by wq on 2017/6/10.
 * 设置
 */
public class SetFragment extends BaseFragment implements View.OnClickListener, OnItemClickListener {
    private Context context;
    @Bind(R.id.click_ok)
    ImageView selectOkBtn;
    @Bind(R.id.next_user_edit)
    RelativeLayout clickUserLy;
    @Bind(R.id.click_set)
    RelativeLayout clickSet;
    @Bind(R.id.click_import_book)
    RelativeLayout clickImportBook;
    @Bind(R.id.click_font)
    RelativeLayout clickFont;
    @Bind(R.id.click_cache)
    RelativeLayout clickCache;
    @Bind(R.id.click_help)
    RelativeLayout clickHelp;
    @Bind(R.id.click_about)
    RelativeLayout clickAbout;
    @Bind(R.id.click_declaration)
    RelativeLayout clickDeclaration;
    @Bind(R.id.user_phone)
    TextView userPhone;
    @Bind(R.id.click_exit)
    RelativeLayout exitBtn;
    @Bind(R.id.click_import_localbook)
    RelativeLayout click_import_localbook;
    private int select = 1;
    private PowerManager powerManager = null;
    private PowerManager.WakeLock wakeLock = null;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_set;
    }

    @Override
    public void initDatas() {
        context = getActivity();
    }

    @Override
    public void configViews() {
        selectOkBtn.setOnClickListener(this);
        clickUserLy.setOnClickListener(this);
        clickSet.setOnClickListener(this);
        clickImportBook.setOnClickListener(this);
        click_import_localbook.setOnClickListener(this);
        clickFont.setOnClickListener(this);
        clickCache.setOnClickListener(this);
        clickHelp.setOnClickListener(this);
        clickAbout.setOnClickListener(this);
        exitBtn.setOnClickListener(this);
        clickDeclaration.setOnClickListener(this);
        powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
        wakeLock.setReferenceCounted(false);
        wakeLock.release();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.click_exit:
                new AlertView("提示", "是否要退出", "取消", new String[]{"确定"}, null, getActivity(), AlertView.Style.Alert, this).setCancelable(true).show();
                break;
            case R.id.click_ok:
                if (select == 1) {
                    select = 0;
                    selectOkBtn.setImageResource(R.mipmap.set_on_img);
                    wakeLock.release();
                } else {
                    select = 1;
                    selectOkBtn.setImageResource(R.mipmap.set_off_img);
                    wakeLock.acquire();
                }
                break;
            case R.id.next_user_edit:
                if (SharedPreferencesUtil.getInstance().getString("Token") == null || SharedPreferencesUtil.getInstance().getString("Token").equals("")) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.click_set:

                break;
            case R.id.click_import_book:
                WifiBookActivity.startActivity(getActivity());
                break;
            case R.id.click_import_localbook:
                Intent intent = new Intent(getActivity(), FileSystemActivity.class);
                startActivity(intent);
                break;
            case R.id.click_font:
                Intent fontIntent = new Intent(getActivity(), FontActivity.class);
                startActivity(fontIntent);
                break;
            case R.id.click_cache:
                Intent cacheIntent = new Intent(getActivity(), CacheActivity.class);
                startActivity(cacheIntent);
                break;
            case R.id.click_help:
                Intent aboutIntent = new Intent(getActivity(), ReadHelpActivity.class);
                aboutIntent.putExtra("title", "使用帮助");
                //aboutIntent.putExtra("url", UeHttpUrl.about);
                startActivity(aboutIntent);
                break;
            case R.id.click_about:
                Intent in = new Intent(getActivity(), AboutActivity.class);
                startActivity(in);
                break;
            case R.id.click_declaration:
                Intent declarationIntent = new Intent(getActivity(), DisclaimerActivity.class);
                declarationIntent.putExtra("title", "负责声明");
                //aboutIntent.putExtra("url", UeHttpUrl.about);
                startActivity(declarationIntent);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!(SharedPreferencesUtil.getInstance().getString(Constant.phoneNum, "") == null || SharedPreferencesUtil.getInstance().getString(Constant.phoneNum, "").equals(""))) {
            userPhone.setText("用户名: " + SharedPreferencesUtil.getInstance().getString(Constant.phoneNum, ""));
        }
        if (SharedPreferencesUtil.getInstance().getString(Constant.Token) == null || SharedPreferencesUtil.getInstance().getString(Constant.Token).equals("")) {
            exitBtn.setVisibility(View.GONE);
        } else {
            exitBtn.setVisibility(View.VISIBLE);
        }
        MobclickAgent.onPageStart("个人中心");
        MobclickAgent.onResume(context);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("个人中心");
        MobclickAgent.onPause(context);
    }
    @Override
    public void onItemClick(Object o, int position) {
        //判断是否是拓展窗口View，而且点击的是非取消按钮
        if (position == -1) {
            return;
        } else {
            if (position == 0) {
                SharedPreferencesUtil.getInstance().putString("Token", "");
                userPhone.setText("");
                exitBtn.setVisibility(View.GONE);
            }
        }
    }
}
